home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 1 / Mac Magazin and MacEasy Magazine CD - Issue 01.iso / Sharewarebibliothek / Powermac / C64 / SOURCE / AppleFloppy.c < prev    next >
Text File  |  1994-06-06  |  7KB  |  321 lines

  1. /*
  2.     Commodore 64 Emulator v0.4      Earle F. Philhower III 
  3.     Copyright (C) 1993-4            (st916w9r@dunx1.ocs.drexel.edu)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include <Controls.h>
  21. #include "Processor.h"
  22. #include "Resources.h"
  23. #include "Error.h"
  24. #include "FileTypes.h"
  25.  
  26. typedef struct {
  27.     long size;
  28.     byte name[24];
  29.     unsigned short type;
  30. } CommieEntry;
  31.  
  32. static WindowPtr dirWind, updt;
  33. static ControlHandle dirScroll;
  34. static CommieEntry dirEntry[144];
  35. static int dirEntries;
  36. static byte header[17], id[3];
  37. static char typeStr[5][4]={"\pUNK", "\pSEQ", "\pPRG", "\pUSR", "\pREL"};
  38.  
  39. #ifdef __MWERKS__
  40. static ControlActionUPP myCntlAction = nil;
  41. #endif
  42.  
  43. static int CollectEntries(CommieEntry entry[]);
  44. static void Xlate2P(byte *in, byte *out);
  45. pascal void DirScrollProc(ControlHandle ctl, int code);
  46.  
  47. int DirInitialize(void)
  48. {
  49.     Rect scrollRect;
  50.     
  51.     /* Read in the directory listing window */
  52.     dirWind=GetNewWindow(kDirList, nil, (WindowPtr)-1L);
  53.     if (dirWind==nil) return kMissingResource;
  54.     SetWRefCon(dirWind, kDirWindow);
  55.     
  56.     /* Add the scroll bar on the right side */
  57.     scrollRect=dirWind->portRect;
  58.     scrollRect.top--;
  59.     scrollRect.bottom++;
  60.     scrollRect.left=scrollRect.right-16+1;
  61.     scrollRect.right++;
  62.     dirScroll=NewControl(dirWind, &scrollRect, "\p", TRUE, 1, 1, 1,
  63.                         scrollBarProc, 0L);
  64.     
  65. #ifdef __MWERKS__
  66.     myCntlAction = NewControlActionProc( DirScrollProc );
  67. #endif
  68.     
  69.     return kNoError;
  70. }
  71.  
  72.  
  73. void FloppyDirectory(void)
  74. {
  75.     dirEntries=CollectEntries(dirEntry);
  76.     if (dirEntries==0) return;
  77.     SetWTitle(dirWind, header);
  78.  
  79.     SetCtlMax(dirScroll, dirEntries);
  80.     SetCtlValue(dirScroll, 1);
  81.     
  82.     SelectWindow(dirWind);
  83.     ShowWindow(dirWind);
  84. }
  85.  
  86.  
  87. void RedrawDir(void)
  88. {
  89.     int x, width, sizeWidth, nameWidth, spaceWidth, bot, start;
  90.     Str255 temp;
  91.     
  92.     SetPort(dirWind);
  93.     EraseRect(&dirWind->portRect);
  94.     DrawControls(dirWind);
  95.     
  96.     TextFont(monaco);
  97.     TextSize(9);
  98.     sizeWidth=StringWidth("\p 999");
  99.     nameWidth=StringWidth("\pXXXXXXXXXXXXXXXXXX");
  100.     spaceWidth=StringWidth("\p ");
  101.     
  102.     for (x=GetCtlValue(dirScroll)-1, start=0; x<dirEntries; x++, start++)
  103.     {
  104.         bot=start*12+12;
  105.         
  106.         NumToString(dirEntry[x].size, temp);
  107.         width=StringWidth(temp);
  108.         MoveTo(4+sizeWidth-width, bot);
  109.         DrawString(temp);
  110.         
  111.         MoveTo(4+sizeWidth+spaceWidth, bot);
  112.         DrawString(dirEntry[x].name);
  113.         
  114.         MoveTo(4+sizeWidth+2*spaceWidth+nameWidth, bot);
  115.         DrawString((unsigned char *)typeStr[dirEntry[x].type]);
  116.     }
  117. }
  118.  
  119. static int CollectEntries(CommieEntry commie[])
  120. {
  121.     byte buff[256], *item;
  122.     int entry, i;
  123.     
  124.     ExternalRead(buff, 18, 0);
  125.     Xlate2P(buff+0x90, header);
  126.     id[0]=2;
  127.     id[1]=buff[0xa2];
  128.     id[2]=buff[0xa3];
  129.     
  130.     entry=0;
  131.     while ((buff[0])&&(entry<144))
  132.     {
  133.         if (ExternalRead(buff, buff[0], buff[1])!=0) return 0;
  134.         for (i=0; i<8; i++) 
  135.         {
  136.             item=&buff[i*32];
  137.             if (item[0x02]&128) 
  138.             {
  139.                 commie[entry].size=item[0x1e]+(((word)item[0x1f])<<8);
  140.                 Xlate2P(item+0x05, commie[entry].name);
  141.                 switch (item[0x02])
  142.                 {
  143.                     case 129:
  144.                         commie[entry].type=1;
  145.                         break;
  146.                     case 130:
  147.                         commie[entry].type=2;
  148.                         break;
  149.                     case 131:
  150.                         commie[entry].type=3;
  151.                         break;
  152.                     case 132:
  153.                         commie[entry].type=4;
  154.                         break;
  155.                     default:
  156.                         commie[entry].type=0;
  157.                         break;
  158.                 }
  159.                 entry++;
  160.             }                
  161.         }
  162.     }
  163.     return entry;
  164. }
  165.  
  166. static void Xlate2P(byte *in, byte *out)
  167. {
  168.     int i;
  169.  
  170.     i=0;
  171.     out[1]='"';
  172.     while (in[i]!=0xa0)
  173.     {
  174.         out[i+2]=in[i];
  175.         i++;
  176.         if (i>=16) break;
  177.     }
  178.     out[i+2]='"';
  179.     out[0]=i+2;
  180. }
  181.  
  182.  
  183. pascal void DirScrollProc(ControlHandle ctl, int code)
  184. {
  185.     int cur, max, min;
  186.     RgnHandle rgn, clip;
  187.     Rect rect;
  188.     
  189.     max=GetCtlMax(ctl);
  190.     min=GetCtlMin(ctl);
  191.     cur=GetCtlValue(ctl);
  192.     switch (code)
  193.     {
  194.         case inPageDown:
  195.         case inDownButton:
  196.             if (cur<max)
  197.             {
  198.                 cur++;
  199.                 rect=dirWind->portRect;
  200.                 rect.right-=16;
  201.                 rgn=NewRgn();
  202.                 clip=NewRgn();
  203.                 GetClip(clip);
  204.                 ScrollRect(&rect, 0, -12, rgn);
  205.                 SetCtlValue(ctl, cur);
  206.                 SetClip(rgn);
  207.                 RedrawDir();
  208.                 SetClip(clip);
  209.                 DisposeRgn(rgn);
  210.                 DisposeRgn(clip);
  211.             }
  212.             break;
  213.         case inPageUp:
  214.         case inUpButton:
  215.             if (cur>min)
  216.             {
  217.                 cur--;
  218.                 rect=dirWind->portRect;
  219.                 rect.right-=16;
  220.                 rgn=NewRgn();
  221.                 clip=NewRgn();
  222.                 GetClip(clip);
  223.                 ScrollRect(&rect, 0, +12, rgn);
  224.                 SetCtlValue(ctl, cur);
  225.                 SetClip(rgn);
  226.                 RedrawDir();
  227.                 SetClip(clip);
  228.                 DisposeRgn(rgn);
  229.                 DisposeRgn(clip);
  230.             }
  231.             break;
  232.     }
  233. }
  234.  
  235. #define sq(x) ((x)*(x))
  236.  
  237. void FloppyClick(EventRecord evt)
  238. {
  239.     Point pt;
  240.     short part;
  241.     ControlHandle ctl;
  242.     int which;
  243.     static long lastClickTime;
  244.     static Point lastClickPoint;
  245.  
  246.     pt=evt.where;
  247.     SetPort(dirWind);
  248.     GlobalToLocal(&pt);
  249.     
  250.     part=FindControl(pt, dirWind, &ctl);
  251.     if (part==0)
  252.     {
  253.         if (((evt.when-lastClickTime)>GetDblTime()) ||
  254.             ((sq(lastClickPoint.h-evt.where.h)+sq(lastClickPoint.v-evt.where.v))
  255.             >4))
  256.         {
  257.             lastClickTime=evt.when;
  258.             lastClickPoint=evt.where;
  259.             return;
  260.         }
  261.         which=pt.v/12;
  262.         which+=GetCtlValue(dirScroll)-1;
  263.         if (which<dirEntries)
  264.         {
  265.             StandardFileReply reply;
  266.             byte buff[17], *name, data;
  267.             int x;
  268.             long size; short fNum;
  269.             byte Read1541(), Open1541();
  270.             
  271.             name=dirEntry[which].name;
  272.             for (x=1; x<(name[0]-1); x++) buff[x]=PetConv(name[x+1]);
  273.             buff[0]=name[0]-2;
  274.             
  275.             StandardPutFile("\pSave File As:", buff, &reply);
  276.             if (reply.sfGood)
  277.             {
  278.                 FSpCreate(&reply.sfFile, (OSType)APPLTYPE, (OSType)HDFTYPE, 0);
  279.                 FSpOpenDF(&reply.sfFile, fsRdWrPerm, &fNum);
  280.                 name=dirEntry[which].name;
  281.                 for (x=1; x<(name[0]-1); x++) buff[x]=name[x+1];
  282.                 buff[0]=name[0]-2;
  283.                 if (Open1541((char *)(&buff[1]), (int)buff[0], 0)!=0) return;
  284.                 size=1;
  285.                 while(Read1541(&data, 0)==0) FSWrite(fNum, &size, &data);
  286.                 FSClose(fNum);
  287.                 Close1541(0);
  288.             }
  289.         }
  290.         else
  291.         {
  292.             SysBeep(1);
  293.             return;
  294.         }
  295.         return;
  296.     }
  297.     
  298.     if (part==inThumb)
  299.     {
  300.         TrackControl(dirScroll, pt, nil);
  301.         RedrawDir();
  302.     }
  303. #ifndef __MWERKS__
  304.     else TrackControl(dirScroll, pt, &DirScrollProc);
  305. #else
  306.     else TrackControl(dirScroll, pt, myCntlAction);
  307. #endif
  308. }
  309.  
  310.  
  311. void FloppyAlert(Str255 str)
  312. {
  313.     /* Store that string in our dialog ParamText (^0) */
  314.     if (str[0]!=0) ParamText(str, "\p", "\p", "\p");
  315.     else ParamText("\pUnknown error condition.", "\p", "\p", "\p");
  316.     
  317.     /* Display the alert. When user clicks OK return */
  318.     Alert(kFloppyAlert, nil);
  319. }
  320.  
  321.